#ifndef WSYSPHERE_H
#define WSYSPHERE_H

#include "geometry/geometry.h"

#define _USE_MATH_DEFINES
#include <math.h>
#include <QList>

/** 王诗阳的球体 */
class WSYSphere: public Geometry {
public:
    WSYSphere(double radius, int widthSegment, int heightSegment) {
        this->setType("object");
        this->initialize();
        this->radius = radius;
        this->widthSegment = widthSegment;
        this->heightSegment = heightSegment;
        this->generate();
    }
    virtual void generate() {
        double widthAngle = 2 * M_PI / widthSegment; //长度分段
        double heightAngle = M_PI / heightSegment; //高度分段
        QList<QList<Vertex3D*>*> *verticalArray = new QList<QList<Vertex3D*>*>();

        for (int h = 0; h <= heightSegment; h++) {
            double phi = heightAngle * h;
            QList<Vertex3D*> *horizontalArray = new QList<Vertex3D*>();
            for (int w = 0; w < widthSegment; w++) {
                double theta = widthAngle * w;

                double cTheta = cos(theta), sTheta = sin(theta);
                double cPhi = cos(phi), sPhi = sin(phi);

                double x = radius * cTheta * sPhi;
                double y = radius * sTheta * sPhi;
                double z = radius * cPhi;

                Vertex3D *sVertex = new Vertex3D(x, y, z);
                horizontalArray->push_back(sVertex);
            }
            verticalArray->push_back(horizontalArray);
        }

        qDebug() << "Total length: " << verticalArray->count();

        for (int h = 0; h < heightSegment; h++) {
            QList<Vertex3D*> *thisVertexArray = verticalArray->at(h);
            QList<Vertex3D*> *nextVertexArray = verticalArray->at(h + 1);

            if (h == 0) { //顶部部分
                Vertex3D *vA = thisVertexArray->at(0);

                int nvCount = nextVertexArray->count();
                for (int n = 0; n < nvCount; n++) {
                    Vertex3D *vB = nextVertexArray->at(n),
                            *vC = nextVertexArray->at((n + 1) % nvCount);
                    this->pushSinglePlane(vA, vB, vC);
                }
            } else if (h == heightSegment - 1) { //底部部分
                Vertex3D *vA = nextVertexArray->at(0);

                int tvCount = thisVertexArray->count();
                for (int t = 0; t < tvCount; t++) {
                    Vertex3D *vC = thisVertexArray->at(t),
                            *vB = thisVertexArray->at((t + 1) % tvCount);
                    this->pushSinglePlane(vA, vB, vC);
                }
            } else { //中部部分
                int tvCount = thisVertexArray->count();
                int nvCount = nextVertexArray->count();
                //qDebug() << "level: " << tvCount << " " << nvCount;
                if (tvCount == nvCount) {
                    for (int t = 0; t < tvCount; t++) {
                        Vertex3D *vA = thisVertexArray->at(t),
                                *vD = thisVertexArray->at((t + 1) % tvCount),
                                *vB = nextVertexArray->at(t),
                                *vC = nextVertexArray->at((t + 1) % tvCount);
                        this->pushSinglePlane(vA, vB, vC);
                        this->pushSinglePlane(vC, vD, vA);
                    }
                }
            }
        }

        qDebug() << "众说纷纭" << this->faces->count();
        this->generateBufferOfObject();
    }

private:
    double radius;
    int widthSegment = 32, heightSegment = 16;};

#endif // WSYSPHERE_H
